﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Lib
{
    public class TransactionData
    {
        /// <summary>
        /// Tên của Data
        /// </summary>
        public string DataName { get; set; }
        /// <summary>
        /// Danh sách thuộc tính của Data
        /// </summary>
        public List<DataAttribute> Attributes { get; set; }
        /// <summary>
        /// Danh sách dữ liệu của Data
        /// </summary>
        public List<Transaction> Transactions { get; set; }
        /// <summary>
        /// Số giao dịch
        /// </summary>
        public int NumTrans { get { return Transactions.Count; } }


        public TransactionData(string name, List<DataAttribute> attribute)
        {
            DataName = name;
            Attributes = attribute;
            Transactions = new List<Transaction>();
        }

        public TransactionData(string name, TransactionData trandata)
        {
            DataName = name;
            Attributes = trandata.Attributes;
            Transactions = new List<Transaction>();
        }

        public TransactionData(List<DataAttribute> attribute)
        {
            Attributes = attribute;
            Transactions = new List<Transaction>();
        }

        public TransactionData()
        {
            Attributes = new List<DataAttribute>();
            Transactions = new List<Transaction>();
        }

        /// <summary>
        /// Lấy giao dịch tại vị trí key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Transaction GetTransaction(int key)
        {
            return Transactions.Find(x => x.Key == key);
        }
        /// <summary>
        /// Thêm giao dịch
        /// </summary>
        /// <param name="tran"></param>
        public void Add(Transaction tran)
        {
            Transactions.Add(tran);
        }
        /// <summary>
        /// Tìm thuộc tính tại vị trí index
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DataAttribute GetAttribute(string name)
        {
            return Attributes.Find(x => x.AttributeName == name);
        }

        public DataAttribute GetAttribute(int index)
        {
            return Attributes.Find(x => x.Index == index);
        }

        public List<TransactionValue> GetAttributeValue(string attname)
        {
            DataAttribute att = Attributes.Find(x => x.AttributeName == attname);
            return att.AttValue;
        }


        public DataTable ConvertToDataTable(TransactionData data)
        {
            DataTable dt = new DataTable();
            List<DataAttribute> atts = data.Attributes;
            List<Transaction> vals = data.Transactions;

            foreach (var att in atts)
            {
                dt.Columns.Add(att.AttributeName);
            }
            foreach (var val in vals)
            {
                TransactionValue[] transValue = val.Value.ToArray();
                var bindable = from name in transValue select name.Name;
                dt.Rows.Add(bindable.ToArray());
            }
            return dt;
        }




        
    }
}
